package com.dunqi.gpm.cangxi.mine.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.ComObserver;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.common.util.SignatureUtil;
import com.dunqi.gpm.cangxi.mine.bean.User;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public class NotifyPasswordActivity extends BaseAct implements TextWatcher {


    @BindView(R.id.old_password_edit)
    EditText oldPasswordEdit;
    @BindView(R.id.new_password_edit)
    EditText newPasswordEdit;
    @BindView(R.id.new_repeat_password_edit)
    EditText newRepeatPasswordEdit;
    @BindView(R.id.config_btn)
    Button configBtn;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {

        oldPasswordEdit.addTextChangedListener(this);
        newPasswordEdit.addTextChangedListener(this);
        newRepeatPasswordEdit.addTextChangedListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notify_password;
    }


    @OnClick(R.id.config_btn)
    public void onViewClicked() {
        if (canCommit()) {
            commit();
        }
    }

    private void commit() {
        String oldPassword = oldPasswordEdit.getText().toString();
        final String newPassword = newPasswordEdit.getText().toString();

        final ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在修改...");
        ApiFactory.getInstance()
                .modifyPwd(SignatureUtil.digest(oldPassword, null).toLowerCase(),
                        SignatureUtil.digest(newPassword, null).toLowerCase())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap((Function<Data, ObservableSource<Data<User>>>) data -> {
                    String resultMessage = data.getResultMessage();
                    if (!TextUtils.isEmpty(resultMessage)) {
                        CommonUtil.showSingleToast(resultMessage);
                        throw new IOException(resultMessage);
                    }
                    User user = UserUtil.getUser();
                    String loginAccount = user.getLoginAccount();
                    String digestPassword = SignatureUtil.digest(newPassword, null).toLowerCase();
                    return ApiFactory.getInstance()
                            .login(loginAccount, digestPassword, "kong", CommonUtil.getDeviceId());
                })
                .compose(RxSchedulers.<Data<User>>io_main())
                .subscribe(new ComObserver<User>() {
                    @Override
                    public void onSuccess(User user) {
                        UserUtil.saveUser(user);
                        finish();
                        CommonUtil.showSingleToast("修改成功");
                    }

                    @Override
                    public void onFinal() {
                        dialog.dismiss();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (canCommit()) {
            configBtn.setBackgroundResource(R.drawable.blue_radius);
        } else {
            configBtn.setBackgroundResource(R.drawable.gray_radius);
        }
    }

    public boolean canCommit() {
        if (TextUtils.isEmpty(oldPasswordEdit.getText())) {
            return false;
        }

        String newPassword = newPasswordEdit.getText().toString();
        if (TextUtils.isEmpty(newPassword)) {
            return false;
        }
        String newRepeatPassword = newRepeatPasswordEdit.getText().toString();
        if (TextUtils.isEmpty(newRepeatPassword)) {
            return false;
        }

        if (!newPassword.equals(newRepeatPassword)) {
            return false;
        }
        return true;
    }

}
